/*    */ package cn.hefan.utils;
/*    */ 
/*    */ import com.mchange.v2.c3p0.ComboPooledDataSource;
/*    */ import java.sql.Connection;
/*    */ import java.sql.ResultSet;
/*    */ import java.sql.SQLException;
/*    */ import java.sql.Statement;
/*    */ import javax.sql.DataSource;
/*    */ 
/*    */ public class DataSourceUtils {
/* 14 */   private static DataSource dataSource = (DataSource)new ComboPooledDataSource();
/*    */   
/* 16 */   private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
/*    */   
/*    */   public static DataSource getDataSource() {
/* 20 */     return dataSource;
/*    */   }
/*    */   
/*    */   public static Connection getConnection() throws SQLException {
/* 26 */     Connection con = tl.get();
/* 27 */     if (con == null) {
/* 28 */       con = dataSource.getConnection();
/* 29 */       tl.set(con);
/*    */     } 
/* 31 */     return con;
/*    */   }
/*    */   
/*    */   public static void startTransaction() throws SQLException {
/* 36 */     Connection con = getConnection();
/* 37 */     if (con != null)
/* 38 */       con.setAutoCommit(false); 
/*    */   }
/*    */   
/*    */   public static void rollback() throws SQLException {
/* 44 */     Connection con = getConnection();
/* 45 */     if (con != null)
/* 46 */       con.rollback(); 
/*    */   }
/*    */   
/*    */   public static void commitAndRelease() throws SQLException {
/* 52 */     Connection con = getConnection();
/* 53 */     if (con != null) {
/* 54 */       con.commit();
/* 55 */       con.close();
/* 56 */       tl.remove();
/*    */     } 
/*    */   }
/*    */   
/*    */   public static void closeConnection() throws SQLException {
/* 62 */     Connection con = getConnection();
/* 63 */     if (con != null)
/* 64 */       con.close(); 
/*    */   }
/*    */   
/*    */   public static void closeStatement(Statement st) throws SQLException {
/* 69 */     if (st != null)
/* 70 */       st.close(); 
/*    */   }
/*    */   
/*    */   public static void closeResultSet(ResultSet rs) throws SQLException {
/* 75 */     if (rs != null)
/* 76 */       rs.close(); 
/*    */   }
/*    */ }
